<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>视频播放器</title>
  <style>
    html, body { width: 100%; height: 100%; margin: 0; padding: 0; background-color: #000; overflow: hidden; }
    #videoElement { width: 100%; height: 100%; }
    .message-overlay { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); color: white; font-size: 16px; text-align: center; padding: 20px; background-color: rgba(0,0,0,0.5); border-radius: 5px; }
  </style>
  <script src="https://cdn.jsdelivr.net/npm/hls.js@1.1.5/dist/hls.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/flv.js@1.6.2/dist/flv.min.js"></script>
</head>
<body>
  <div id="message" class="message-overlay">加载中...</div>
  <video id="videoElement" controls autoplay muted style="display:none;"></video>

  <script>
    const videoElement = document.getElementById('videoElement');
    const messageElement = document.getElementById('message');
    let currentPlayer = null;

    function getUrlParams() {
      const params = new URLSearchParams(window.location.search);
      return {
        src: params.get('src'),
        type: params.get('type') || 'auto',
        hasAudio: params.get('hasAudio') === 'true'
      };
    }

    function showMessage(msg, isError = false) {
      messageElement.textContent = msg;
      messageElement.style.display = 'block';
      messageElement.style.color = isError ? '#f56c6c' : 'white';
      videoElement.style.display = 'none';
      if (isError) {
         try {
            window.parent.postMessage({ type: 'playError', message: msg }, '*');
         } catch(e) {
            console.error("postMessage to parent failed", e);
         }
      }
    }
    
    function hideMessage() {
        messageElement.style.display = 'none';
        videoElement.style.display = 'block';
    }

    function destroyPlayer() {
      if (currentPlayer) {
        if (currentPlayer.destroy) {
          try {
            currentPlayer.destroy();
          } catch(e) {
            console.error("Player destroy failed", e);
          }
        }
        currentPlayer = null;
      }
    }

    function initPlayer() {
      destroyPlayer();
      const params = getUrlParams();
      const src = params.src;

      if (!src) {
        showMessage('错误: 缺少视频源地址', true);
        return;
      }

      let type = params.type;
      if (type === 'auto') {
          if (src.includes('.m3u8')) type = 'hls';
          else if (src.includes('.flv') || src.startsWith('ws')) type = 'flv';
          else if (src.includes('.mp4')) type = 'native';
          else {
              showMessage('错误: 无法根据URL确定视频类型', true);
              return;
          }
      }

      showMessage('加载中...');

      if (type === 'hls') {
        if (Hls.isSupported()) {
          const hls = new Hls({
              // 超时时间
              manifestLoadingTimeOut: 10000,
              fragLoadingTimeOut: 15000
          });
          currentPlayer = hls;
          hls.loadSource(src);
          hls.attachMedia(videoElement);
          hls.on(Hls.Events.MEDIA_ATTACHED, function () {
            hideMessage();
            videoElement.play().catch(e => showMessage('播放失败: ' + e.message, true));
          });
          hls.on(Hls.Events.ERROR, function (event, data) {
            if (data.fatal) {
              showMessage(`播放错误: ${data.details} (${data.type})`, true);
              destroyPlayer();
            }
          });
        } else if (videoElement.canPlayType('application/vnd.apple.mpegurl')) {
            videoElement.src = src;
            videoElement.addEventListener('loadedmetadata', function() {
                hideMessage();
                videoElement.play().catch(e => showMessage('播放失败: ' + e.message, true));
            });
            videoElement.addEventListener('error', function() {
                showMessage('视频加载失败', true);
            });
        } else {
          showMessage('您的浏览器不支持HLS播放', true);
        }
      } else if (type === 'flv') {
        if (flvjs.isSupported()) {
          const flvPlayer = flvjs.createPlayer({
            type: 'flv',
            isLive: true,
            url: src,
            hasAudio: params.hasAudio
          });
          currentPlayer = flvPlayer;
          flvPlayer.attachMediaElement(videoElement);
          flvPlayer.load();
          
          let playFired = false;
          const playAction = () => {
              if(playFired) return;
              playFired = true;
              hideMessage();
              flvPlayer.play().catch(e => showMessage('播放失败: ' + e.message, true));
          };

          flvPlayer.on(flvjs.Events.METADATA_ARRIVED, playAction);
          // Fallback for streams without metadata
          setTimeout(playAction, 1000);

          flvPlayer.on(flvjs.Events.ERROR, function(errorType, errorDetail) {
              showMessage(`播放错误: ${errorDetail} (${errorType})`, true);
              destroyPlayer();
          });
        } else {
          showMessage('您的浏览器不支持FLV播放', true);
        }
      } else if (type === 'native') {
          videoElement.src = src;
          videoElement.addEventListener('loadedmetadata', function() {
              hideMessage();
              videoElement.play().catch(e => showMessage('播放失败: ' + e.message, true));
          });
          videoElement.addEventListener('error', function() {
              showMessage('视频加载失败', true);
          });
      } else {
        showMessage(`不支持的视频类型: ${type}`, true);
      }
    }

    window.onload = initPlayer;
  </script>
</body>
</html>
